package com.rfid.project.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

import biz.util.*;

import com.rfid.project.model.*;
import com.rfid.project.dao.*;
import com.mysema.query.BooleanBuilder;

import java.util.List;

@Service
@Transactional
public class ProjectStakeholderService{

	@Autowired
	private ProjectStakeholderDao projectStakeholderDao;
	
	private QProjectStakeholder $ = QProjectStakeholder.projectStakeholder;
	
	@Autowired
	public void setProjectStakeholderDao(ProjectStakeholderDao projectStakeholderDao) {
		this.projectStakeholderDao = projectStakeholderDao;
	}

	@Autowired
	private PeopleDao peopleDao;

	private QPeople $_people = QPeople.people;

	@Autowired
	public void setPeopleDao(PeopleDao peopleDao) {
		this.peopleDao = peopleDao;
	}
	
	public List<ProjectStakeholder> findAll(){
		return ListUtil.toList(projectStakeholderDao.findAll());
	}
				
	public List<ProjectStakeholder> findAll(
	Long projectId,
	String department,
	String name,
	String post,
	String personalTel,
	String officeTel,
	String remark,
	Long projectStakeholderTypeId,
	String orderByAttribute	){
	BooleanBuilder condition = new BooleanBuilder();
	
	if(IdUtil.isNotEmpty(projectId)){
		condition.and($.project.id.eq(projectId));
	}
	
	if(StringUtil.isNotEmpty(department)){
		condition.and($.department.contains(department));
	}
	
	if(StringUtil.isNotEmpty(name)){
		condition.and($.name.contains(name));
	}
	
	if(StringUtil.isNotEmpty(post)){
		condition.and($.post.contains(post));
	}
	
	if(StringUtil.isNotEmpty(personalTel)){
		condition.and($.personalTel.contains(personalTel));
	}
	
	if(StringUtil.isNotEmpty(officeTel)){
		condition.and($.officeTel.contains(officeTel));
	}
	
	if(StringUtil.isNotEmpty(remark)){
		condition.and($.remark.contains(remark));
	}
	
	if(IdUtil.isNotEmpty(projectStakeholderTypeId)){
		condition.and($.projectStakeholderType.id.eq(projectStakeholderTypeId));
	}
	
	
	return ListUtil.toList(projectStakeholderDao.findAll(condition));
	}		

	public Page<ProjectStakeholder> findAll(
			Long projectId,
			String department,
			String name,
			String post,
			String personalTel,
			String officeTel,
			String remark,
			Long projectStakeholderTypeId,
			Boolean personID,
			int pageNumber, int pageSize,String sortAttribute,String orderDirection) {
			BooleanBuilder condition = new BooleanBuilder();
	
			if(IdUtil.isNotEmpty(projectId)){
				condition.and($.project.id.eq(projectId));
			}
			if(StringUtil.isNotEmpty(department)){
				condition.and($.department.contains(department));
			}
			if(StringUtil.isNotEmpty(name)){
				condition.and($.name.contains(name));
			}
			if(StringUtil.isNotEmpty(post)){
				condition.and($.post.contains(post));
			}
			if(StringUtil.isNotEmpty(personalTel)){
				condition.and($.personalTel.contains(personalTel));
			}
			if(StringUtil.isNotEmpty(officeTel)){
				condition.and($.officeTel.contains(officeTel));
			}
			if(StringUtil.isNotEmpty(remark)){
				condition.and($.remark.contains(remark));
			}
			
			if(IdUtil.isNotEmpty(projectStakeholderTypeId)){
				condition.and($.projectStakeholderType.id.eq(projectStakeholderTypeId));
			}
			
			if (null != personID) {
				if (personID) {
					condition.and(this.$.personID.isNotNull());
				} else {
					condition.and(this.$.personID.isNull());
				}
			}
			
			Sort sortObject = null;
			if(StringUtil.isEmpty(sortAttribute)){
				sortObject = new Sort(Direction.DESC, "id");
			}else{
				sortObject = new Sort(Direction.fromStringOrNull(orderDirection),sortAttribute);
			}
			
			PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize, sortObject);
			return projectStakeholderDao.findAll(condition, pageRequest);
	}
	
	
	
	public ProjectStakeholder getProjectStakeholder(Long id){
		return projectStakeholderDao.getOne(id);
	}
	
	public void deleteProjectStakeholder(Long id){
		projectStakeholderDao.delete(id);
	}

	public void saveProjectStakeholder(ProjectStakeholder projectStakeholder) {
		projectStakeholderDao.save(projectStakeholder);
	}

	public ProjectStakeholder findOne(Long id) {
		return this.projectStakeholderDao.findOne(id);
	}

}
		
